<?php
/**
 * 招聘信息管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-31
 */
namespace Admin\Controller;

class RecruitController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db       =new \Model\RecruitModel();
        $this->db_job   =new \Model\RecruitsJobTypeModel();
        $this->db_salary=new \Model\RecruitsSalaryTypeModel();
    }
    
    /*
     * 已审核的招聘信息列表
     * 
     * @return #
     */
    public function index(){
        if(I("request.name")){
            $map['name']=I("request.name");
        }
        $map    =$this->_search("Recruit");
        if(I("request.regionId")){
            $map['regionId']=I("request.regionId");
        }
        if(session('type')<2){
            $map['companyId']   =session("authId");
        }        
        if(I("request.province")){
            $map['regionId']    =I("post.province","","int");
        }
        if(I("request.city")){
            $map['regionId']    =I("post.city","","int");
        }
        $map['state']   =2;
        $sortBy         ="top";
        $sort           =desc;
        $infoList       =$this->_list($this->db,$map,$sortBy,$sort);
        $provinceData = M('Area')->where("id like '__0000' and state='CN'")->select();
        $this->assign ('provinceData', $provinceData);
        $this->assign("infoList",$infoList);
        $this->assign("act",1);
        $this->display();
    }
    
    /*
     * 待审的招聘信息
     * 
     * @return #
     */
    public function auditRecruit(){
        $map    =$this->_search("Recruit");
        if(session('type')<2){
            $map['companyId']   =session("authId");
        }        
        if(I("request.province")){
            $map['regionId']    =I("post.province","","int");
        }
        if(I("request.city")){
            $map['regionId']    =I("post.city","","int");
        }
        $map['state']   =1;
        $sortBy         ="top";
        $sort           =desc;
        $infoList       =$this->_list($this->db,$map,$sortBy,$sort);
        $provinceData = M('Area')->where("id like '__0000' and state='CN'")->select();
        $this->assign ('provinceData', $provinceData);
        $this->assign("infoList",$infoList);
        $this->assign("act",2);
        $this->display('index');
    }
    
    /*
     * 显示编辑招聘信息（包括新增和修改）
     * 
     * @return #
     */
    function show(){
        $job    =D("RecruitsJobType")->select();
        $salary =D("RecruitsSalaryType")->select();
        $provinceData = M('Area')->where("id like '__0000' and state='CN'")->select();
        $this->assign ('provinceData', $provinceData);
        $this->assign("salary",$salary);
        $this->assign("job",$job);
        //$this->assign("region",$region);
        if(I("request.id","","int")){
            $recruitInfo=$this->db->find(I("request.id","","int"));
            $this->assign("recruitInfo",$recruitInfo);
            //获取地区
            if (!empty($recruitInfo['regionid'])){
                $areaInfo = M('Area')->where(array("id"=>$recruitInfo['regionid']))->find();
            }
            if ($areaInfo){
                //获取城市
                $code = substr($areaInfo['id'], 0, 2);
                $cityData = M('Area')->where("id like '{$code}__00' and state='CN' and id<>'{$code}0000'")->select();
                $this->assign ('cityData', $cityData);
                //获取地区
                $code = substr($areaInfo['id'], 0, 4);
                $areaData = M('Area')->where("id like '{$code}__' and state='CN' and id<>'{$code}00'")->select();
                $this->assign ('areaData', $areaData);
                $this->assign('areaInfo', $areaInfo);
            }
        }
        $this->display();
    }
    
    /*
     * 编辑招聘信息
     * 
     * @return #
     */
    public function edit(){
        if(I("request.id")){
            $map['id']      =I("request.id","","int");
        }
        if(I("request.name")){
            $map['name']    =I("request.name","","trim");
        }else{
            $this->error("招聘名称不能为空");
        }
        if(I("request.recruitNum")){
            $map['recruitNum']  =I("request.recruitNum","","trim");
        }else{
            $this->error("招聘人数不能为空");
        }
        if(I("request.top")){
            $map['top_sign']=1;
        }
        if(I("request.companyTel")){
            $map['companyTel']  =I("request.companyTel");
        }else{
            $this->error("联系电话不能为空");
        }
        if(I("request.companyName")){
            $map['companyName'] =I("request.companyName");
        }else{
            $this->error("公司名称不能为空");
        }
        if(I("request.province")){
            $map['regionId']    =I("post.province","","int");
        }
        if(I("request.city")){
            $map['regionId']    =I("post.city","","int");
        }
        if(I("request.country")){
            $map['regionId']    =I("post.country","","int");
        }
        $map['jobType']     =I("request.jobType","","int");
        $map['salaryType']  =I("request.salaryType","","trim");
        if($this->db->edit($map)){
            $this->success("招聘信息编辑成功");
        }else{
            $this->error("招聘信息编辑失败");
        }        
    }
    
    /*
     * 审核、批量审核招聘信息
     * 
     * @return bool
     */
    public function alledit(){       
        $map['id']      =I("request.id");
        $map['state']   =2;
        if($this->db->edits($map)){            
            $this->success("审核成功",U("index"));
        }else{
            $this->error("审核失败",U("index"));
        }
    }
    
    /*
     * 待审核，批量待审核招聘信息
     * 
     * @return #
     */
    public function audit(){
        $map['id']      =I("request.id");
        $map['state']   =1;
        if($this->db->edits($map)){            
            $this->success("待审核成功",U("index"));
        }else{
            $this->error("待审核失败",U("index"));
        }
    }
    
    /*
     * 删除，批量删除招聘信息
     * 
     * @return #
     */
    public function del(){
        $map['id']      =I("request.id");
        $map['state']   =3;
        if($this->db->edits($map)){            
            $this->success("删除成功",U("index"));
        }else{
            $this->error("删除失败",U("index"));
        }
    }
    
    /*
     * 岗位列表
     * 
     * @return #
     */
    public function jobList(){
        if(I("request.jobName")){
            $map['jobName'] =I("request.jobName");
        }
        $map    =$this->_search("RecruitsJobType");
        $sortBy ="jobId";
        $sort   =desc;
        $jobList=$this->_list($this->db_job, $map,$sortBy,$sort,"jobId");
        $this->assign("list",$jobList);
        $this->display();
    }
    
    /*
     * 显示编辑页面
     * 
     * @return #
     */
    public function jobShow(){
        if(I("request.id")){
            $jobInfo        =$this->db_job->find(I("request.id"));
            $this->assign("jobInfo",$jobInfo);
        }
        $this->display();        
    }
    
    /*
     * 编辑岗位
     * 
     * @return #
     */
    public function jobEdit(){
        if(I("request.id")){
            $map['jobId']   =I("request.id","","int");
        }
        if(I("request.jobName")){
            $map['jobName'] =I("request.jobName",'','trim');
        }else{
            $this->error("岗位名称不能为空");
        }
        if($this->db_job->edit($map)){
            $this->success("岗位编辑成功");
        }else{
            $this->error("岗位编辑失败");
        }
    }
    
    /*
     * 删除、批量删除岗位
     * 
     * @return #
     */
    public function delJob(){
        if(I("request.id")){
            $map['jobId']   =I("request.id");
        }else{
            $this->error("参数错误");
        }
        if($this->db_job->del($map)){
            $this->success("岗位删除成功");
        }else{
            $this->error("岗位删除失败");
        }
    }
    
    /*
     * 薪水范围列表
     * 
     * @return #
     */
    public function salaryList(){
        if(I("request.salaryName")){
            $map['salaryName']  =I("request.salaryName");
        }
        $map    =$this->_search("RecruitsSalaryType");
        $sortBy ="salaryId";
        $sort   =desc;
        $salList=$this->_list($this->db_salary, $map,$sortBy,$sort,"salaryId");
        $this->assign("list",$salList);
        $this->display();
    }
    
    /*
     * 显示编辑页面
     * 
     * @return #
     */
    public function salaryShow(){
        if(I("request.id")){
            $salaryInfo        =$this->db_salary->find(I("request.id"));
            $this->assign("salaryInfo",$salaryInfo);
        }
        $this->display();        
    }
    
    /*
     * 编辑薪资范围
     * 
     * @return #
     */
    public function salaryEdit(){
        if(I("request.id")){
            $map['salaryId']   =I("request.id","","int");
        }
        if(I("request.salaryName")){
            $map['salaryName'] =I("request.salaryName",'','trim');
        }else{
            $this->error("薪水范围不能为空");
        }
        if($this->db_salary->edit($map)){
            $this->success("薪水编辑成功");
        }else{
            $this->error("薪水编辑失败");
        }
    }
    
    /*
     * 删除、批量删除薪资范围
     * 
     * @return #
     */
    public function delSalary(){
        if(I("request.id")){
            $map['salaryId']    =I("request.id");
        }else{
            $this->error("参数错误");
        }
        if($this->db_salary->del($map)){
            $this->success("薪水删除成功");
        }else{
            $this->error("薪水删除失败");
        }
    }
}